package com.lazy.mongo.shw;

import org.bson.types.ObjectId;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * @author hadoop
 *
 */
public class DBObjectUtil {

    /**
     * 通过Plan获得BasicDBObject对象
     * 
     * @param mac
     */
    public static BasicDBObject getDBObjectByPlan(PosData plan){
        ObjectId id;
        String sId = plan.getHostid();
        if(sId == null || sId.length() != 24){
            id = new ObjectId();
        }else{
            id = new ObjectId(sId);
        }
        BasicDBObject doc = new BasicDBObject("_id", id);
        doc.append("hostno", plan.getHostno());
        return doc;
    }

    /**
     * 通过BasicDBObject获得Plan对象
     * 
     * @param object
     */
    public static PosData getPlanByDBObject(DBObject object){
        PosData plan = new PosData();
        plan.setId(((ObjectId)object.get("_id")).toString());
        plan.setHostid(((String)object.get("hostid")).toString());
        plan.setHostno((String)object.get("hostno"));
        return plan;
    }
}

